// LemCoop
// Copyright (C) 2013  Oliver Naumann <bldrax@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "level.h"

Level::Level()
{
	m_mask = nullptr;
	number_of_lems = 0;
}

Level::~Level()
{
	// Delete the entries.
	for (std::vector<Entry*>::iterator it = entries.begin(); it != entries.end(); it++)
		delete *it;

	entries.clear();

	// Delete the graphics and ground mask.
	al_destroy_bitmap(m_graphics);
	delete m_mask;
}

void Level::OpenEntries()
{
	for (std::vector<Entry*>::iterator it = entries.begin(); it != entries.end(); it++)
		(*it)->Open();
}

void Level::StartToRelease()
{
	for (std::vector<Entry*>::iterator it = entries.begin(); it != entries.end(); it++)
		(*it)->set_spawn_lems(true);
}

char Level::GetGroundMaskValue(int x, int y)
{
	// Return the mask value if it could be obtained, else presume there is air.
	char value;
	if (m_mask->GetPixel(x, y, value))
		return value;

	return GroundMask::AIR;
}

void Level::Update(float timing_factor)
{
	// Update entries.
	for (std::vector<Entry*>::iterator it = entries.begin(); it != entries.end(); it++)
		(*it)->Update(timing_factor);
}

void Level::Draw()
{
	// Draw level graphics.
	al_draw_bitmap(m_graphics, 0, 0, NULL);

	// Draw entries.
	for (std::vector<Entry*>::iterator it = entries.begin(); it != entries.end(); it++)
		(*it)->Draw();
}